package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.SubsidyRemindAddDTO;
import com.oa.admin.dto.SubsidyRemindListDTO;
import com.oa.admin.entity.SubsidyRemindStep;
import com.oa.admin.entity.SubsidyRemindStepFile;
import com.oa.admin.entity.TransactionFile;
import com.oa.admin.service.ISubsidyRemindStepFileService;
import com.oa.admin.service.ISubsidyRemindStepService;
import com.oa.admin.service.ITransactionFileService;
import com.oa.admin.vo.SubsidyRemindDetails;
import com.oa.admin.vo.SubsidyRemindListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.SubsidyRemind;
import com.oa.admin.mapper.SubsidyRemindMapper;
import com.oa.admin.service.ISubsidyRemindService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 补贴提醒 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class SubsidyRemindServiceImpl extends ServiceImpl<SubsidyRemindMapper, SubsidyRemind> implements ISubsidyRemindService {

    private final SubsidyRemindMapper subsidyRemindMapper;

    @Autowired
    public SubsidyRemindServiceImpl(SubsidyRemindMapper subsidyRemindMapper) {
        this.subsidyRemindMapper = subsidyRemindMapper;
    }

    @Autowired
    private ISubsidyRemindStepService subsidyRemindStepService;

    @Autowired
    private ISubsidyRemindStepFileService subsidyRemindStepFileService;

    @Autowired
    private ITransactionFileService transactionFileService;

    @Override
    @CommonService
    public PageResult<SubsidyRemind> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<SubsidyRemind> list = subsidyRemindMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<SubsidyRemind> page = new Page<>(pageNum, pageSize);
            //查询
            List<SubsidyRemind> list = subsidyRemindMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public SubsidyRemindDetails findSubsidyRemindById(String id){
        return subsidyRemindMapper.findSubsidyRemindById(id);
    }

    @Override
    @CommonService
    public PageResult<SubsidyRemindListVO> findSubsidyRemindList(SubsidyRemindListDTO subsidyRemindListDTO) {
        Page page = subsidyRemindListDTO.getPage();
        return new PageResult<>(subsidyRemindMapper.findSubsidyRemindList(page, subsidyRemindListDTO));
    }

    @Transactional
    @Override
    @CommonService
    public Resp add(SubsidyRemindAddDTO subsidyRemindAddDTO) {
        SubsidyRemind subsidyRemind = new SubsidyRemind();
        BeanUtils.copyProperties(subsidyRemindAddDTO, subsidyRemind);
        if(insert(subsidyRemind)){
            TransactionFile transactionFile = new TransactionFile();
            BeanUtils.copyProperties(subsidyRemindAddDTO.getTransactionFile(), transactionFile);
            transactionFile.setRemindId(subsidyRemind.getId());
            transactionFileService.insert(transactionFile);

            if(subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS() != null && subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS().size() > 0){
                subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS().forEach(d -> {
                    SubsidyRemindStep subsidyRemindStep = new SubsidyRemindStep();
                    BeanUtils.copyProperties(d, subsidyRemindStep);
                    subsidyRemindStep.setSubsidyRemindId(subsidyRemind.getId());
                    subsidyRemindStepService.insert(subsidyRemindStep);

                    SubsidyRemindStepFile subsidyRemindStepFile = new SubsidyRemindStepFile();
                    BeanUtils.copyProperties(d.getSubsidyRemindStepFileAddDTO(), subsidyRemindStepFile);
                    subsidyRemindStepFile.setSubidyRemindStepId(subsidyRemindStep.getId());
                    subsidyRemindStepFileService.insert(subsidyRemindStepFile);
                });
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Transactional
    @Override
    @CommonService
    public Resp edit(SubsidyRemindAddDTO subsidyRemindAddDTO) {
        SubsidyRemind subsidyRemind = new SubsidyRemind();
        BeanUtils.copyProperties(subsidyRemindAddDTO, subsidyRemind);
        if(updateById(subsidyRemind)){
            TransactionFile transactionFile = new TransactionFile();
            BeanUtils.copyProperties(subsidyRemindAddDTO.getTransactionFile(), transactionFile);
            transactionFile.update(new EntityWrapper().eq("remind_id", subsidyRemind.getId()));

            if(subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS() != null && subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS().size() > 0){
                subsidyRemindStepService.delete(new EntityWrapper().eq("subsidy_remind_id", subsidyRemind.getId()));
                subsidyRemindAddDTO.getSubsidyRemindStepAddDTOS().forEach(d -> {
                    SubsidyRemindStep subsidyRemindStep = new SubsidyRemindStep();
                    BeanUtils.copyProperties(d, subsidyRemindStep);
                    subsidyRemindStep.setSubsidyRemindId(subsidyRemind.getId());
                    subsidyRemindStepService.insert(subsidyRemindStep);

                    subsidyRemindStepFileService.delete(new EntityWrapper().eq("subidy_remind_step_id", subsidyRemindStep.getId()));
                    SubsidyRemindStepFile subsidyRemindStepFile = new SubsidyRemindStepFile();
                    BeanUtils.copyProperties(d.getSubsidyRemindStepFileAddDTO(), subsidyRemindStepFile);
                    subsidyRemindStepFile.setSubidyRemindStepId(subsidyRemindStep.getId());
                    subsidyRemindStepFileService.insert(subsidyRemindStepFile);
                });
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Override
    public void export(SubsidyRemindListDTO subsidyRemindListDTO, HttpServletResponse response) {
        List<SubsidyRemindListVO> subsidyRemindListVOS;
        if(subsidyRemindListDTO.getPage() != null){
            Page page = subsidyRemindListDTO.getPage();
            subsidyRemindListVOS = subsidyRemindMapper.findSubsidyRemindList(page, subsidyRemindListDTO);
        }else{
            subsidyRemindListVOS = subsidyRemindMapper.findSubsidyRemindList(new Page(), subsidyRemindListDTO);
        }
        String[] rows = {"id", "类别", "公司名称", "办理时间", "截止日期"};
        try {
            ExcelUtil.downloadExcel("补贴提醒列表" + DateUtil.getDayMonth(), rows, subsidyRemindListVOS, "补贴提醒列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
